//
//  CommunityViewController.m
//  FlowerRainGround
//
//  Created by lanou on 16/4/19.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "CommunityViewController.h"
#import "HeaderView.h"
#import "CycleScrollView.h"
#import "MyCollectionReusableView.h"

#import "MyPostCollectionView.h"
#import "CircleModel.h"
#import "TopicModel.h"
#import "RecommendModel.h"
#import "CustomerModel.h"
#import "FactoryCollectionViewCell.h"
#import "BaseCollectionViewCell.h"
#import "LORefresh.h"
#import "DetailPostViewController.h"
#import "HotTopicScrollView.h"
#import "ThemeViewController.h"
#import "HotTopicViewController.h"
#import "HotTopicTableViewCell.h"

@interface CommunityViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UICollectionView *collectionView;//集合视图
@property (nonatomic, strong)UIScrollView *scrollView;//滚动视图

@property (nonatomic) NSInteger pageindex;
@property (nonatomic, strong) NSMutableArray *circleArray;//轮播数据源
@property (nonatomic, strong) NSMutableArray *hotTopicArray;//热门话题数据源
@property (nonatomic, strong) NSMutableArray *recommendArray;//推荐动态数据源
@property (nonatomic, strong)CycleScrollView *cycleScrollView;//轮播视图
@property (nonatomic, strong)UIScrollView *hotTopicScrollView;//热门话题滚动视图
@property (nonatomic)NSInteger pageSize;
@property (nonatomic)NSInteger currentPageIndex;
@property (nonatomic, strong) NSMutableArray *picArray;
@property (nonatomic, assign) float h;


@property (nonatomic, strong)UIView *view1;
@property (nonatomic, strong)UIView *view2;
@property (nonatomic, strong)UIView *view3;
@property (nonatomic, strong)UIView *view4;

@property (nonatomic, strong) UITableView *addTimeTableView;
@property (nonatomic, strong) UITableView *ringTableView;
@property (nonatomic, strong) UITableView *attentionTableView;
@property (nonatomic, strong) NSMutableArray *addtimeArray;
@property (nonatomic, strong) NSMutableArray *ringArray;
@property (nonatomic, strong) NSMutableArray *attrntionArray;

@property (nonatomic, strong)NSMutableArray *zfcArray;
@end

@implementation CommunityViewController


#pragma mark --- 懒加载 ---

- (NSMutableArray *)circleArray {
    
    if (!_circleArray) {
        self.circleArray = [NSMutableArray array];
    }
    
    return _circleArray;
}

- (NSMutableArray *)hotTopicArray {
    
    if (!_hotTopicArray) {
        self.hotTopicArray = [NSMutableArray array];
    }
    
    return _hotTopicArray;
}

- (NSMutableArray *)recommendArray {
    
    if (!_recommendArray) {
        self.recommendArray = [NSMutableArray array];
    }
    
    return _recommendArray;
}

- (NSMutableArray *)addtimeArray {
    
    if (!_addtimeArray) {
        self.addtimeArray = [NSMutableArray array];
    }
    
    return _addtimeArray;
}

- (NSMutableArray *)ringArray {
    
    if (!_ringArray) {
        self.ringArray = [NSMutableArray array];
    }
    
    return _ringArray;
}

- (NSMutableArray *)attrntionArray {
    
    if (!_attrntionArray) {
        self.attrntionArray = [NSMutableArray array];
    }
    
    return _attrntionArray;
}

- (NSMutableArray *)picArray {
    
    if (!_picArray) {
        self.picArray = [NSMutableArray array];
    }
    
    return _picArray;
}

- (NSMutableArray *)zfcArray {
    
    if (!_zfcArray) {
        self.zfcArray = [NSMutableArray array];
    }
    
    return _zfcArray;
}

#pragma mark --- 数据处理 ---
//轮播数据加载
- (void)handleCircleData {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://m.htxq.net/servlet/SysAdvertisingServlet?action=getAdList" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *totalDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
//                NSLog(@"totalDic:%@",totalDic);
        
        for (NSDictionary *modelDic in totalDic[@"result"]) {
            
            CircleModel *circleModel = [[CircleModel alloc] init];
            [circleModel setValuesForKeysWithDictionary:modelDic];
            [self.circleArray addObject:circleModel];
            
        }
        
//                NSLog(@"circleArray:%@", self.circleArray);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
}

//热门话题数据加载
- (void)handleHotData {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://m.htxq.net/servlet/SysTagsServlet?action=getTagsList&currentPageIndex=0&pageSize=10" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *hotTopicDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
//                NSLog(@"hotTopicDic:%@", hotTopicDic);
        
        for (NSDictionary *modelDic in hotTopicDic[@"result"]) {
            
            TopicModel *topicModel = [[TopicModel alloc] init];
            [topicModel setValuesForKeysWithDictionary:modelDic];
            [self.hotTopicArray addObject:topicModel];
            
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.collectionView reloadData];
            [self.collectionView.defaultFooter endRefreshing];
            [self.collectionView.defaultHeader endRefreshing];
            
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"error:%@",error);
        
    }];
    
}

//推荐动态数据加载
- (void)handleRecommendData {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//        manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    
    [manager GET:@"http://app.htxq.net/servlet/UserBbsServlet?type=%E8%8D%90&action=getJianOrJingList&currentPageIndex=0" parameters:@{@"pageSize":@(self.pageSize)} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *recommendDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"recommendDic:%@", recommendDic);
        
        [self.recommendArray removeAllObjects];
        for (NSDictionary *modelDic in recommendDic[@"result"]) {
            
            RecommendModel *recommendModel = [[RecommendModel alloc] init];
            [recommendModel setValuesForKeysWithDictionary:modelDic];
            [self.recommendArray addObject:recommendModel];
            
            CustomerModel *customerModel = [[CustomerModel alloc] init];
            [customerModel setValuesForKeysWithDictionary:modelDic[@"customer"]];
            recommendModel.customerModel = customerModel;
            
        }
        //        NSLog(@"recommendArray:%@",self.recommendArray);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.collectionView reloadData];
            
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"error:%@", error);

    }];
    
    
}

//最新数据加载
- (void)handleAddtimeData {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://app.htxq.net/servlet/UserBbsServlet?userId=2ac739cd-bd7d-4de0-bbb8-e85a17230b8a&action=getNewestList&currentPageIndex=0" parameters:@{@"pageSize":@(self.pageSize)} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *addtimeDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"&&&&&&&&&&&&%@",addtimeDic);
        
        [self.addtimeArray removeAllObjects];
        for (NSDictionary *modelDic in addtimeDic[@"result"]) {
            
            RecommendModel *recommendModel = [[RecommendModel alloc] init];
            [recommendModel setValuesForKeysWithDictionary:modelDic];
            [self.addtimeArray addObject:recommendModel];
            
            CustomerModel *customerModel = [[CustomerModel alloc] init];
            [customerModel setValuesForKeysWithDictionary:modelDic[@"customer"]];
            recommendModel.customerModel = customerModel;
        }
//        NSLog(@"^^^^^^%@",self.addtimeArray);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.addTimeTableView reloadData];
            
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

//圈子数据加载
- (void)handleRingData {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://app.htxq.net/servlet/UserBbsServlet?currentPageIndex=0&type=f528fe59-2c7c-4831-9f4a-cd50f8247b01&action=getBbsCircle" parameters:@{@"pageSize":@(self.pageSize),@"userId":[userManager getUsrID]} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *ringDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"^^^^%@",ringDic);
        
        [self.ringArray removeAllObjects];
        for (NSDictionary *modelDic in ringDic[@"result"]) {
            
            RecommendModel *recommendModel = [[RecommendModel alloc] init];
            [recommendModel setValuesForKeysWithDictionary:modelDic];
            [self.ringArray addObject:recommendModel];
            
            CustomerModel *customerModel = [[CustomerModel alloc] init];
            [customerModel setValuesForKeysWithDictionary:modelDic[@"customer"]];
            recommendModel.customerModel = customerModel;
        }
//        NSLog(@"^^^^^%@",self.ringArray);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.ringTableView reloadData];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

//关注数据加载
- (void)handleAttentionData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://app.htxq.net/servlet/UserBbsServlet?action=findAttentionUserBBS&currentPageIndex=0" parameters:@{@"userId":[userManager getUsrID], @"pageSize":@(self.pageSize)} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *attentionDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"^^^^^^^^%@", attentionDic);
        
        [self.attrntionArray removeAllObjects];
        for (NSDictionary *modelDic in attentionDic[@"result"]) {
            
            RecommendModel *recommendModel = [[RecommendModel alloc] init];
            [recommendModel setValuesForKeysWithDictionary:modelDic];
            [self.attrntionArray addObject:recommendModel];
            
            CustomerModel *customerModel = [[CustomerModel alloc] init];
            [customerModel setValuesForKeysWithDictionary:modelDic[@"customer"]];
            recommendModel.customerModel = customerModel;
        }
//        NSLog(@"^^^^^^%@",self.attrntionArray);
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.attentionTableView reloadData];
            
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"error:%@", error);
        
    }];
    
}

#pragma mark --- viewDidLoad ---

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.translucent = NO;
    self.pageSize = 10;
    self.navigationItem.title = @"社区";
    [self createNavigationBar];

    [self handleHotData];
    [self handleCircleData];
    [self handleRecommendData];
    [self handleAddtimeData];
    [self handleRingData];
    [self handleAttentionData];
    
    [self createCollectionView];
    [self refresh];

}


#pragma mark --- 上拉刷新/下拉加载 ---

- (void)refresh {
    
    __block typeof(self) myself = self;
    
    //发现下拉刷新
    [self.collectionView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        
        _pageSize = 6;
        [myself.collectionView.defaultHeader endRefreshing];
        [myself handleRecommendData];
        
    }];
    
    
    //最新下拉刷新
    [self.addTimeTableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        
        _pageSize = 6;
        [myself.addTimeTableView.defaultHeader endRefreshing];
        [myself handleAddtimeData];
        
    }];
    
    
    //圈子下拉刷新
    [self.ringTableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        
        _pageSize = 6;
        [myself.ringTableView.defaultHeader endRefreshing];
        [myself handleRingData];
        
    }];
    
    
    //关注下拉刷新
    [self.attentionTableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        
        _pageSize = 6;
        [myself.attentionTableView.defaultHeader endRefreshing];
        [myself handleAttentionData];
        
    }];

    
    
    //发现上拉加载
    [self.collectionView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        
        _pageSize += 12;
        [myself.collectionView.defaultFooter endRefreshing];
        [myself handleRecommendData];
        
    }];
    
    
    //最新上拉加载
    [self.addTimeTableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        
        _pageSize += 12;
        [myself.addTimeTableView.defaultFooter endRefreshing];
        [myself handleAddtimeData];
        
    }];
    
    
    //圈子上拉加载
    [self.ringTableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        
        _pageSize += 12;
        [myself.ringTableView.defaultFooter endRefreshing];
        [myself handleRingData];
        
    }];
    
    
    //关注上拉加载
    [self.attentionTableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        
        _pageSize += 12;
        [myself.attentionTableView.defaultFooter endRefreshing];
        [myself handleAttentionData];
        
    }];
    
}


#pragma mark --- 视图创建 ---


//创建导航栏
- (void)createNavigationBar {
    
    NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:nil options:nil];
    HeaderView *headerView = [views lastObject];
    headerView.frame = CGRectMake(0, 0, kWidth + 40, 44);
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    btn.frame = CGRectMake(300, 12, 20, 20);
//    btn.backgroundColor = [UIColor orangeColor];
//    [btn addTarget:self action:@selector(messButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    //消息按钮
    self.navigationItem.titleView = headerView;
    self.navigationController.navigationBar.translucent = NO;
    
    headerView.SendButton = ^(UIButton *sender) {
        
        if (sender.tag == 1000) {
            [self createCollectionView];
            [self refresh];

        } else if (sender.tag == 1001){
            [self createAddtimeTableView];
            [self refresh];

        } else if (sender.tag == 1002){
            [self createCircleTableView];
            [self refresh];

        } else {
            [self createAttentionTableView];
            [self refresh];
        }
    };

}

//创建最新界面表视图
- (void)createAddtimeTableView {
    
    _view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49 - 64)];
    _view2.backgroundColor = [UIColor greenColor];
    
    self.addTimeTableView  = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49 - 64)];
    self.addTimeTableView.delegate = self;
    self.addTimeTableView.dataSource = self;
    [self.addTimeTableView registerClass:[HotTopicTableViewCell class] forCellReuseIdentifier:@"addTimeTableView"];
    
    [self.view2 addSubview:self.addTimeTableView];
    [self.view addSubview:self.view2];
    
}

//创建圈子界面表视图
- (void)createCircleTableView {
    
    _view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49 - 64)];
    _view3.backgroundColor = [UIColor grayColor];

    self.ringTableView  = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49 - 64)];
    self.ringTableView.delegate = self;
    self.ringTableView.dataSource = self;
    [self.ringTableView registerClass:[HotTopicTableViewCell class] forCellReuseIdentifier:@"ringTableView"];
    
    
    [self.view3 addSubview:self.ringTableView];
    [self.view addSubview:self.view3];

}

//创建关注界面表视图
- (void)createAttentionTableView {
    
    _view4 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49 - 64)];
    _view4.backgroundColor = [UIColor whiteColor];

    self.attentionTableView  = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49 - 64)];
    self.attentionTableView.delegate = self;
    self.attentionTableView.dataSource = self;
    [self.attentionTableView registerClass:[HotTopicTableViewCell class] forCellReuseIdentifier:@"attentionTableView"];
    
    [self.view4 addSubview:self.attentionTableView];
    [self.view addSubview:self.view4];

}




//创建集合视图
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(kWidth/2 - 4, kWidth/2);
    layout.minimumLineSpacing = 2;
    layout.minimumInteritemSpacing = 2;
    
    //设置集合视图的滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
    layout.headerReferenceSize = CGSizeMake(kWidth, kHeight * 2 / 3);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49 - 64) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    //注册集合视图cell
    [_collectionView registerNib:[UINib nibWithNibName:@"RecommendCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([RecommendModel class])];
    
    //注册增广视图头视图
    [_collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    _view1 = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _view1.backgroundColor = [UIColor whiteColor];
    [_view1 addSubview:_collectionView];
    [self.view addSubview:_view1];
    
}


//创建轮播视图
- (void)createScrollView {
    
    //通过封装的CycleScrollView创建无限轮播视图
    _cycleScrollView = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight / 3) animationDuration:2.0];
    NSMutableArray *viewsArray = [NSMutableArray array];
    for (int i = 0; i < self.circleArray.count; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:_cycleScrollView.bounds];
        
        CircleModel *circleModel = self.circleArray[i];
//        NSLog(@"%@",self.circleArray);
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:circleModel.imageUrl]];
        [viewsArray addObject:imageView];
    }
    _cycleScrollView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex) {
        return viewsArray[pageIndex];
    };
    
    //确定轮播图片数量
    _cycleScrollView.totalPagesCount = viewsArray.count;
    
    //这个block完成点击轮播图片的操作
    __block typeof(self)myself = self;
    _cycleScrollView.TapActionBlock = ^(NSInteger pageIndex) {
        
        ThemeViewController *themeVC = [[ThemeViewController alloc] init];
        themeVC.circleArray = myself.circleArray;
        themeVC.pageIndex = pageIndex;
        [myself.navigationController pushViewController:themeVC animated:YES];
        
    };
}


#pragma mark --- collectionView代理方法 ---
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.recommendArray.count;
    
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row < self.recommendArray.count) {//防止数组越界引起的闪退
    BaseModel *recommendModel = self.recommendArray[indexPath.row];
    
    //通过工厂模式创建cell, 并给cell赋值
    BaseCollectionViewCell *cell = [FactoryCollectionViewCell createCollectionViewCellWithModel:recommendModel collection:collectionView indexPath:indexPath];
    [cell setDataWithModel:recommendModel];
        return cell;
    }
    return nil;
    
}


//头尾视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    //确定集合视图上方的视图都为集合视图的头视图，包括轮播视图、两个Label、一个热门话题的滚动视图
        MyCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [self createScrollView];
        [view addSubview:self.cycleScrollView];
    
    UILabel *topiclabel =[[UILabel alloc] initWithFrame:CGRectMake(0 , kHeight / 3, kWidth, 30)];
    topiclabel.text = @"热门话题";
    [view addSubview:topiclabel];
    
    
       //封装在HotTopicScrollView类里面
    HotTopicScrollView *hotTopicScrollView = [[HotTopicScrollView alloc] initWithFrame:CGRectMake(0, kHeight / 3 + 30, kWidth, kHeight / 3 - 60)];
    hotTopicScrollView.backgroundColor = [UIColor greenColor];
    if (self.hotTopicArray.count > 0) {
        [hotTopicScrollView createScrollViewWithHotTopicArray:self.hotTopicArray];
        [view addSubview:hotTopicScrollView];
    }
    
    
    //点击热门话题跳转到热门话题列表界面
    __block typeof(self)myself = self;
    hotTopicScrollView.TapActionBlock = ^(UIImageView *imageView) {

        HotTopicViewController *hotTopicVC = [[HotTopicViewController alloc] init];
        
        hotTopicVC.str = [self.hotTopicArray[imageView.tag - 3000] name];
        [myself.navigationController pushViewController:hotTopicVC animated:YES];
        
};
    
    
    
    UILabel *recommendLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, kHeight *2/3 - 30, kWidth, 30)];
    recommendLabel.text = @"推荐动态";
    [view addSubview:recommendLabel];
    
        return view;
 
}

//跳转到帖子详情界面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row < self.recommendArray.count) {

    DetailPostViewController *detailVC = [[DetailPostViewController alloc] init];
    detailVC.recommendArray = self.recommendArray;
    detailVC.index = indexPath.row;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    }
}


#pragma mark --- tableView 代理方法 ---


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.addTimeTableView) {
        return self.addtimeArray.count;
    }else if (tableView == self.ringTableView) {
        return self.ringArray.count;
    }else {
        return self.attrntionArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RecommendModel *model = nil;
    if (tableView == self.addTimeTableView) {
      
        model = self.addtimeArray[indexPath.row];
        HotTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addTimeTableView" forIndexPath:indexPath];
        NSArray *subViews = cell.subviews;
        for (UIView *view in subViews) {
            [view removeFromSuperview];
        }
        [cell createCellWithModel:model];
        return cell;

    }else if (tableView == self.ringTableView) {
        
        model = self.ringArray[indexPath.row];
        HotTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ringTableView" forIndexPath:indexPath];
        NSArray *subViews = cell.subviews;
        for (UIView *view in subViews) {
            [view removeFromSuperview];
        }
        [cell createCellWithModel:model];
        return cell;

    }else {
        
        model = self.attrntionArray[indexPath.row];
        HotTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"attentionTableView" forIndexPath:indexPath];
        NSArray *subViews = cell.subviews;
        for (UIView *view in subViews) {
            [view removeFromSuperview];
        }
        [cell createCellWithModel:model];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.picArray removeAllObjects];
    
    RecommendModel *model = nil;
    if (tableView == self.addTimeTableView) {
        model = self.addtimeArray[indexPath.row];
        self.picArray = [[model.attachment componentsSeparatedByString:@","] mutableCopy];
        
        
    }else if (tableView == self.ringTableView) {
        model = self.ringArray[indexPath.row];
        self.picArray = [[model.attachment componentsSeparatedByString:@","] mutableCopy];
        
        
    }else {
        model = self.attrntionArray[indexPath.row];
        self.picArray = [[model.attachment componentsSeparatedByString:@","] mutableCopy];
    }
    
    CGFloat imagewidth = (kWidth-24)/3;
    _h = 0;
    if (self.picArray.count == 1) {
        _h = imagewidth*3 + 164;
    } else if(self.picArray.count == 2) {
        _h = (kWidth-22)/2 + 160;
    }else {
        NSInteger number = [self getRowNumber];
        _h = number * imagewidth + (number-1)*2 + 160;
    }
    
    return _h;
}

//获取列数
- (NSInteger)getRowNumber {
    
    if (self.picArray.count % 3 == 0) {
        return self.picArray.count / 3;
    } else {
        return self.picArray.count / 3 + 1;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
